/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Reading;
import Controller.ReadingDAO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author ngoc
 */
public class Freading extends javax.swing.JFrame {

    /**
     * Creates new form Freading
     */
    
    
    Vector vTenLop = new Vector();
    Vector vUrlRead = new Vector();    
    Vector vMaLop = new Vector();
    Vector vUrlQuestion = new Vector();
    Vector vUrlAnswer = new Vector();
    Vector vTimeAnswer = new Vector();
    
    ReadingDAO rdDAO = new ReadingDAO();
    int NumberQuestion;
    int count = 0;
    String[] arrAnswer;
    
    boolean start = true;
    int hour = 0 ;
    int min = 0 ;
    int sec = 0 ;
    Timer timer;
            
    public Freading() {
        initComponents();
        this.setLocationRelativeTo(null);
        
        btnNext.setVisible(false);
        btnback.setVisible(false);
        txtinputAnswer.setVisible(false);
        
        txtConTentLessonRead.setEditable(false);
        txtConTentLessonRead.setLineWrap(true);
        txtConTentLessonRead.setWrapStyleWord(true);
        
        txtContentQuestion.setEditable(false);
        txtContentQuestion.setLineWrap(true);
        txtContentQuestion.setWrapStyleWord(true);
        
        txtReply.setEditable(false);
        txtReply.setLineWrap(true);
        txtReply.setWrapStyleWord(true);
        txtReply.setColumns(2);
        txtReply.setRows(2);
        
        txtscore.setEditable(false);
        

        
        for (Reading reading : rdDAO.getAll()) {
            vTenLop.add(reading.getNameRead());
            vMaLop.add(reading.getIDRead());
            vUrlRead.add(reading.getUrlRead());
            vUrlQuestion.add(reading.getUrlQuestion());
            vUrlAnswer.add(reading.getUrlAnswer());
            vTimeAnswer.add(reading.getTimeAnswer());
            System.out.println(reading.getTimeAnswer());
        }
        cbchoose.setModel(new DefaultComboBoxModel(vTenLop));
        txtConTentLessonRead.setText(rdDAO.readFile( (String)vUrlRead.get(cbchoose.getSelectedIndex())));
        txtContentQuestion.setText(rdDAO.readFile( (String)vUrlQuestion.get(cbchoose.getSelectedIndex())));
        
        //time
        String time = (String)vTimeAnswer.get(cbchoose.getSelectedIndex());
        
        String[] parts = time.split(":");
        hour =    Integer.parseInt(parts[0]) ;
        min =    Integer.parseInt(parts[1]) ;
        sec =    Integer.parseInt(parts[2]) ;
         
        
        
         timer = new Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
        lbtiem.setText(String.valueOf(hour +" : " + min +" : "+sec));
        if( hour == 0 && min == 0 && sec == 0)
        {
	start = false;
	lbtiem.setText("Hết giờ");
        btnNext.setVisible(false);
        btnback.setVisible(false);
        txtinputAnswer.setVisible(false);
            btnokActionPerformed(e);
	}
        
        sec-=1;
	if(sec < 0 && (min > 0 || hour > 0))
	{ 
	sec = 59;
	min -= 1;
	}
	if(min < 0 && hour > 0)
	{
	min = 59;
	hour -= 1;
	}
            }
        });
	
        try {
            NumberQuestion = rdDAO.countLines((String)vUrlAnswer.get(cbchoose.getSelectedIndex()));
            String a = String.valueOf(count);
            lbNumberQuestion.setText(a);
            arrAnswer = new String[NumberQuestion+1];
            
        } catch (IOException ex) {
            Logger.getLogger(Freading.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbchoose = new javax.swing.JComboBox();
        btnImportFile = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtConTentLessonRead = new javax.swing.JTextArea();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtContentQuestion = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtReply = new javax.swing.JTextArea();
        btnok = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        txtscore = new javax.swing.JTextField();
        btnCancel = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtinputAnswer = new javax.swing.JTextField();
        btnNext = new javax.swing.JButton();
        btnback = new javax.swing.JButton();
        lbNumberQuestion = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbtiem = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        cbchoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbchooseActionPerformed(evt);
            }
        });

        btnImportFile.setText("import file");
        btnImportFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImportFileActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("READING");

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel17.setText("choose");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbchoose, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnImportFile, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(420, 420, 420)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnImportFile)
                            .addComponent(cbchoose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel17, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        txtConTentLessonRead.setBackground(new java.awt.Color(243, 247, 247));
        txtConTentLessonRead.setColumns(20);
        txtConTentLessonRead.setRows(5);
        jScrollPane1.setViewportView(txtConTentLessonRead);

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setText("Content Question");

        txtContentQuestion.setBackground(new java.awt.Color(243, 247, 247));
        txtContentQuestion.setColumns(20);
        txtContentQuestion.setRows(5);
        jScrollPane2.setViewportView(txtContentQuestion);

        txtReply.setBackground(new java.awt.Color(243, 247, 247));
        txtReply.setColumns(20);
        txtReply.setRows(5);
        jScrollPane3.setViewportView(txtReply);

        btnok.setBackground(new java.awt.Color(255, 51, 51));
        btnok.setText("Start");
        btnok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnokActionPerformed(evt);
            }
        });

        jLabel14.setText("Score");

        txtscore.setText("0");

        btnCancel.setBackground(new java.awt.Color(255, 0, 51));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel15.setText("content lesson read");

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel16.setText(" reply");

        btnNext.setBackground(new java.awt.Color(255, 0, 51));
        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnback.setBackground(new java.awt.Color(255, 0, 51));
        btnback.setText("Back");
        btnback.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbackActionPerformed(evt);
            }
        });

        jLabel2.setText("Question");

        jLabel3.setText("Time");

        lbtiem.setText("jLabel4");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(100, 100, 100)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lbNumberQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(btnok, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnback, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtscore, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtinputAnswer, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnNext, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)))
                                .addGap(17, 17, 17))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lbtiem, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel14))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnok)
                        .addGap(27, 27, 27)
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbtiem, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtscore, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbNumberQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtinputAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnback))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 927, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnImportFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImportFileActionPerformed
        // TODO add your handling code here:
        Faddreading r = new Faddreading();
         r.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnImportFileActionPerformed

    private void cbchooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbchooseActionPerformed
        // TODO add your handling code here:
        btnok.setText("Start");
        txtConTentLessonRead.setText(rdDAO.readFile( (String)vUrlRead.get(cbchoose.getSelectedIndex())));
        txtContentQuestion.setText(rdDAO.readFile( (String)vUrlQuestion.get(cbchoose.getSelectedIndex())));
        try {
            NumberQuestion = rdDAO.countLines((String)vUrlAnswer.get(cbchoose.getSelectedIndex()));
            txtinputAnswer.setText("");
            txtReply.setText("");
            String a = String.valueOf(count);
            lbNumberQuestion.setText(a);
            arrAnswer = new String[NumberQuestion+1];
        } catch (IOException ex) {
            Logger.getLogger(Freading.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        //time
        btnNext.setVisible(true);
        btnback.setVisible(true);
        txtinputAnswer.setVisible(true);
        String time = (String)vTimeAnswer.get(cbchoose.getSelectedIndex());
        String[] parts = time.split(":");
        hour =    Integer.parseInt(parts[0]) ;
        min =    Integer.parseInt(parts[1]) ;
        sec =    Integer.parseInt(parts[2]) ;
        lbtiem.setText(String.valueOf(hour +" : " + min +" : "+sec));
    }//GEN-LAST:event_cbchooseActionPerformed

    private void btnbackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbackActionPerformed
        // TODO add your handling code here:
        int num = Integer.parseInt(lbNumberQuestion.getText());
        //System.out.println("so dong file : "+ NumberQuestion);
        if (num <= 0) {
            num = 0;
            String s = String.valueOf(num);
            arrAnswer[num] = s+" "+txtinputAnswer.getText();
        } else{
            num = num-1;
            //////////
            String a = String.valueOf(num+1);
            arrAnswer[num+1] = a+" "+txtinputAnswer.getText();

            ///////////////
            String b;
            if(arrAnswer[num] != null) {
                if (arrAnswer[num].length()>2) {
                String[] parts = arrAnswer[num].split(" ");
                txtinputAnswer.setText(parts[1]);
                }else
                    txtinputAnswer.setText("");
                ////
                b = String.valueOf(num);
                lbNumberQuestion.setText(b);
            }else {
                txtinputAnswer.setText(""); 
                b = String.valueOf(num);
                lbNumberQuestion.setText(b);
            }
        }
        /////
        txtReply.setText("");
        //System.out.println(arrAnswer[num]);
        ///////////////
        for (int i = 0; i< arrAnswer.length; i++) {
            String S = arrAnswer[i]+ "\n";
            if(arrAnswer[i] != null)
                txtReply.append(S);
        }        
    }//GEN-LAST:event_btnbackActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed

        // TODO add your handling code here:
        int num = Integer.parseInt(lbNumberQuestion.getText());
        if (num >= NumberQuestion) {
            num = NumberQuestion;
            String a = String.valueOf(num);
            arrAnswer[num] = a+" "+txtinputAnswer.getText();
        } else{
            num = num+1;
            //////////
            String a = String.valueOf(num-1);
            arrAnswer[num-1] = a+" "+txtinputAnswer.getText();

            ///////////////
            String b;
            if(arrAnswer[num] != null) {
                System.out.println(arrAnswer[num].length());
                if (arrAnswer[num].length()> 2) {
                String[] parts = arrAnswer[num].split(" ");
                txtinputAnswer.setText(parts[1]);
                }else
                    txtinputAnswer.setText("");
                 
                b = String.valueOf(num);
                lbNumberQuestion.setText(b);
            }else {
                txtinputAnswer.setText(""); 
                b = String.valueOf(num);
                lbNumberQuestion.setText(b);
            }
        }
        /////
        txtReply.setText("");
        //System.out.println(arrAnswer[num]);
        ///////////////
        for (int i = 0; i< arrAnswer.length; i++) {
            String S = arrAnswer[i]+ "\n";
            if(arrAnswer[i] != null)
                txtReply.append(S);
        }
            
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        txtReply.setText("");
        lbNumberQuestion.setText("0");
        txtinputAnswer.setText("");
        txtscore.setText("0");
        Arrays.fill( arrAnswer, null );
        String time = (String)vTimeAnswer.get(cbchoose.getSelectedIndex());
        
        String[] parts = time.split(":");
        hour =    Integer.parseInt(parts[0]) ;
        min =    Integer.parseInt(parts[1]) ;
        sec =    Integer.parseInt(parts[2]) ;
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnokActionPerformed
        if(btnok.getText().equals("Ok") )
        {
            btnok.setText("Start");
            btnNext.setVisible(false);
            btnback.setVisible(false);
            txtinputAnswer.setVisible(false);
        try {
            // TODO add your handling code here:
            timer.stop();
            String filename = (String)vUrlAnswer.get(cbchoose.getSelectedIndex());
            String[] arr ;
            arr = rdDAO.readUsingBufferedReader(filename,StandardCharsets.UTF_8);
            String Answer = rdDAO.readFile(filename);
            int count = 0;
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i].equals(arrAnswer[j])) {
                        count = count+1;
                    }
                }
            }
            //System.out.println("giong nhau: " +count);
            rdDAO.infoBox("bạn đã làm đúng được: "+count + "\n đáp án : \n "+ Answer,"thông báo");
            
            String a = String.valueOf(count);
            txtscore.setText(a);
            
            //lbtiem.setText(String.valueOf(hour +" : " + min +" : "+sec));
            
            
        } catch (IOException ex) {
            Logger.getLogger(Freading.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        }else
        if (btnok.getText().equals("Start") ) {
            btnok.setText("Ok");
            btnNext.setVisible(true);
            btnback.setVisible(true);
            txtinputAnswer.setVisible(true);
            String time = (String)vTimeAnswer.get(cbchoose.getSelectedIndex());
            String[] parts = time.split(":");
            hour =    Integer.parseInt(parts[0]) ;
            min =    Integer.parseInt(parts[1]) ;
            sec =    Integer.parseInt(parts[2]) ;
            timer.start();
        }
    }//GEN-LAST:event_btnokActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        formchinh fr = new formchinh();
        fr.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Freading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Freading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Freading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Freading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Freading().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnImportFile;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnback;
    private javax.swing.JButton btnok;
    private javax.swing.JComboBox cbchoose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lbNumberQuestion;
    private javax.swing.JLabel lbtiem;
    private javax.swing.JTextArea txtConTentLessonRead;
    private javax.swing.JTextArea txtContentQuestion;
    private javax.swing.JTextArea txtReply;
    private javax.swing.JTextField txtinputAnswer;
    private javax.swing.JTextField txtscore;
    // End of variables declaration//GEN-END:variables
}
